/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConnectorGUI.java
 *
 * Created on 10.08.2013, 13:23:25
 */
package transgui;

import java.util.LinkedList;

import javax.swing.JOptionPane;

import libraries.TransConn;

/**
 * GUI for mapping EAGLE pins to Modelica connectors
 * @author Andreas Gruner
 */
public class ConnectorGUI extends javax.swing.JFrame {

	LinkedList<ConnectorPanel> connComponents;
	TransConn connectorResult;
	TransGUI parent;
	
    /** Creates new form ConnectorGUI */
    public ConnectorGUI(int connectors, TransGUI parent) {
        initComponents();
        this.parent = parent;
        connComponents = new LinkedList<ConnectorPanel>();
        for (int i = 0; i < connectors; i++) {
        	ConnectorPanel cp = new ConnectorPanel();
        	jPanelConnectors.add(cp);
        	connComponents.add(cp);
        }
    	connectorResult = new TransConn();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanelConnectors = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(700, 400));

        jScrollPane1.setMinimumSize(new java.awt.Dimension(200, 300));

        jPanelConnectors.setLayout(new javax.swing.BoxLayout(jPanelConnectors, javax.swing.BoxLayout.PAGE_AXIS));
        jScrollPane1.setViewportView(jPanelConnectors);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.PAGE_START);

        jButtonOK.setText("Ok");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });
        jPanel2.add(jButtonOK);

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });
        jPanel2.add(jButtonCancel);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
       	if (evt.getSource() == jButtonOK) {
       		boolean valid = true;
       		for (ConnectorPanel c: connComponents) {
       			valid &= c.isValidValue();
       		}
       		if (valid) {
           		for (ConnectorPanel c: connComponents) {
           			String key = c.getKey();
           			String value = c.getValue();
           			connectorResult.put(key, value);
           		}
           		this.dispose();
           		parent.setEnabled(true);
           		parent.finishPartCreation(connectorResult);
       		} else {
       			JOptionPane.showMessageDialog(this, "Please fill in all text fields using no spaces (\" \") or pipes (\"|\").", "Illegal arguments", JOptionPane.ERROR_MESSAGE);
       		}
       	}
    }//GEN-LAST:event_jButtonOKActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
    	parent.setEnabled(true);
    	this.dispose();        
    }//GEN-LAST:event_jButtonCancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelConnectors;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
